/**
 * 
 */
package spatial.geometry;

import java.awt.geom.Point2D;

/**
 * A BiSector is a line function that divides a plane with respect to two
 * points. It is a perpendicularity to the line determined by the two points.
 * 
 * @author wu wei
 * 
 */
public class BiSector extends LineFunction {
	Point2D.Double ref1, ref2;

	private BiSector(double a, double b, double c, LineType lt,
			Point2D.Double ref1, Point2D.Double ref2) {
		super(a, b, c, lt);
		this.ref1 = ref1;
		this.ref2 = ref2;
	}

	public static BiSector getBisector(Point2D.Double ref1, Point2D.Double ref2) {
		double a = 0;
		double b = 0;
		double c = 0;
		LineType lt = LineType.NORMAL;
		LineFunction lf = null;
		try {
			lf = new LineFunction(ref1, ref2);
		} catch (NotDefinedException e) {
			throw e;
		}

		if (lf != null) {
			double middleX = (ref1.getX() + ref2.getX()) / 2;
			double middleY = (ref1.getY() + ref2.getY()) / 2;
			Point2D.Double middle = new Point2D.Double(middleX, middleY);
			if (lf.getLineType() == LineType.NORMAL) {
				// a = -1/lf.a, b = 1, c = by - ax
				b = 1;
				a = -1.0 / lf.getA();
				c = middle.getY() - a * middle.getX();
				lt = LineType.NORMAL;
			}
			if (lf.getLineType() == LineType.XPARALLEL) {
				// yparallel, x=constant
				lt = LineType.YPARALLEL;
				a = 1;
				b = 0;
				c = -1 * middle.getX();
			}
			if (lf.getLineType() == LineType.YPARALLEL) {
				// xparallel, y = constant
				lt = LineType.XPARALLEL;
				a = 0;
				b = 1;
				c = middle.getY();
			}
		}

		return new BiSector(a, b, c, lt, ref1, ref2);

	}

	public Point2D.Double getRefPoint1() {
		return this.ref1;
	}

	public Point2D.Double getRefPoint2() {
		return this.ref2;
	}

	public boolean equals(BiSector anotherBs) {
		LineFunction lf1 = (LineFunction) this;
		LineFunction lf2 = (LineFunction) anotherBs;
		return lf1.equals(lf2);
	}

	public String toString() {
		return "bisector" + this.getUniqueLineFunctionId();
	}
}
